<?php
namespace Orm\Order;

use Base\Exception\RmException;
class OrderClass extends \Base\BaseOrmObject{
    protected   $id,
				$order_code = '', // 订单编码
				$article_id = 0, // 文章表ID
				$seller_uid = 0, // 卖家userID
				$buyer_uid = 0, // 买家userID
				$adds = '', // 地址
				$contact = '', // 联系人
				$tel = '', // 联系电话
				$total_price = 0, // 订单总价
				$remark = '', // 备注
				$pay_status = 0, // 支付状态,0未支付，1已支付
				$pay_datetime = '', // 支付时间
				$appointment_time = '', // 预约时间
				$logistics_name = '', // 物流名称
				$logistics_numbers = '', // 物流单号
				$order_status = '', // 物流状态 默认0 待配送0 配送中1 已完成2
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    /*
     * 订单常量
     */
    const PAID = 1; //已支付
    const NONPAYMENT = 0; //未支付

    const CONTACT_NULL = -1; //联系人不能为空
    const TEL_NULL = -2; //电话
    const APPOINTMENT_TIME_NULL = -3; //预约时间不能为空
    const ADDS_NULL = -4; //地址不能为空
    const ID_NULL = -5; //orderID 不能为空

    const PENDING_DELIVERY = 0; // 待配送
    const IN_DELIVERY = 1; // 配送中
    const COMPLETED = 2; // 已完成

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Order\OrderModel();
        $data = $model->loadById($id);
        self::__init($data);
    }
    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'order_code' => $this->order_code,
			'article_id' => $this->article_id,
			'seller_uid' => $this->seller_uid,
			'buyer_uid' => $this->buyer_uid,
			'adds' => $this->adds,
			'contact' => $this->contact,
			'tel' => $this->tel,
			'total_price' => $this->total_price,
			'remark' => $this->remark,
			'pay_status' => $this->pay_status,
			'pay_datetime' => $this->pay_datetime,
			'appointment_time' => $this->appointment_time,
			'logistics_name' => $this->logistics_name,
			'logistics_numbers' => $this->logistics_numbers,
			'order_status' => $this->order_status,
        );

        $model = new \Orm\Order\OrderModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'order_code' => $this->order_code,
			'article_id' => $this->article_id,
			'seller_uid' => $this->seller_uid,
			'buyer_uid' => $this->buyer_uid,
			'adds' => $this->adds,
			'contact' => $this->contact,
			'tel' => $this->tel,
			'total_price' => $this->total_price,
			'remark' => $this->remark,
			'pay_status' => $this->pay_status,
			'pay_datetime' => $this->pay_datetime,
			'appointment_time' => $this->appointment_time,
			'logistics_name' => $this->logistics_name,
			'logistics_numbers' => $this->logistics_numbers,
            'order_status' => $this->order_status,
        );

        $model = new \Orm\Order\OrderModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Order\OrderModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }

    public static function getOrderList()
    {
        return array(
            self::PENDING_DELIVERY => '待配送',
            self::IN_DELIVERY => '配送中',
            self::COMPLETED => '已完成'
        );
    }

    public static function getOrderById($id)
    {
        $order_status_list = self::getOrderList();
        return isset($order_status_list[$id]) ? $order_status_list[$id] : '未知';
    }

    public static function getPaysTatus()
    {
        return array(
            self::PAID => '已支付',
            self::NONPAYMENT => '未支付'
        );
    }
    public static function getPayById($id)
    {
        $pays_tatus_list = self::getPaysTatus();
        return isset($pays_tatus_list[$id]) ? $pays_tatus_list[$id] : '未知';
    }
}